package com.js.myapp.system.activity;

// Created by JS on 2021/1/22.

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.net.Uri;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import androidx.annotation.Nullable;

import com.js.basicpage.activity.BaseLoadingDialogActivity;
import com.js.basicpage.activity.WatchPictureActivity;
import com.js.basicutils.ImageUtils;
import com.js.basicutils.LogUtils;
import com.js.basicutils.NavigationBarUtil;
import com.js.basicutils.OsUtils;
import com.js.basicutils.PathUtils;
import com.js.basicutils.StatusBarUtil;
import com.js.basicutils.ToastUtils;
import com.js.basicutils.image.ImageLoader;
import com.js.basicutils.image.interfaces.ImgLoadListener;
import com.js.myapp.R;

import java.io.File;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class TakePhotoActivity extends BaseLoadingDialogActivity {

    @BindView(R.id.btn_take_photo)
    Button btnTakePhoto;
    @BindView(R.id.btn_pick_photo)
    Button btnPickPhoto;
    @BindView(R.id.btn_net_photo)
    Button btnNetPhoto;
    @BindView(R.id.btn_crop)
    Button btnCrop;
    @BindView(R.id.iv)
    ImageView iv;

    private TakePhotoActivity mContext;
    private String imagePath;
    private Uri choseUri;
    private boolean isNet;

    private static String netImg = "https://goss.veer.com/creative/vcg/veer/1600water/veer-375318408.jpg";

    public static void start(Context context) {
        Intent intent = new Intent(context, TakePhotoActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_take_photo);
        mContext = this;
        NavigationBarUtil.setSysNavigationColor(mContext, Color.BLUE);
        StatusBarUtil.setStatusBarColor(mContext, R.color.color_statusbar_bg);
        ButterKnife.bind(this);
    }

    @OnClick({R.id.btn_take_photo, R.id.btn_pick_photo, R.id.btn_net_photo, R.id.btn_crop, R.id.iv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_take_photo:
                isNet = false;
                imagePath = PathUtils.randomCacheImage(mContext);
                OsUtils.startSystemCamera(mContext, imagePath);
                break;
            case R.id.btn_pick_photo:
                isNet = false;
                imagePath = PathUtils.randomCacheImage(mContext);
                OsUtils.startImagePicker(mContext);
                break;
            case R.id.btn_net_photo:
                isNet = true;
                imagePath = netImg;
                ImageLoader.get(mContext).display(netImg).into(iv);
                break;
            case R.id.iv:
                if (isNet) {
                    ImageLoader.get(mContext).load(netImg).listener(new ImgLoadListener.LoadListener() {
                        @Override
                        public void success(@Nullable File file) {
                            if (file == null || !file.exists()) {
                                ToastUtils.showToast(mContext, "图片保存失败！");
                                return;
                            }
                            WatchPictureActivity.startActivity(mContext, file.getPath(), "测试照片");
                        }

                        @Override
                        public void fail(@Nullable Exception e) {
                            ToastUtils.showToast(mContext, "图片保存失败！");
                        }
                    }).start();
                } else {
                    if (imagePath != null && !TextUtils.isEmpty(imagePath)) {
                        WatchPictureActivity.startActivity(mContext, imagePath, "测试照片");
                    }
                }
                break;
            case R.id.btn_crop:
                if (isNet) {
                    ImageLoader.get(mContext).load(netImg).listener(new ImgLoadListener.LoadListener() {
                        @Override
                        public void success(@Nullable File file) {
                            if (file == null || !file.exists()) {
                                ToastUtils.showToast(mContext, "图片保存失败！");
                                return;
                            }
                            startCrop(PathUtils.fileToUri(file));
                        }

                        @Override
                        public void fail(@Nullable Exception e) {
                            ToastUtils.showToast(mContext, "图片保存失败！");
                        }
                    }).start();
                    return;
                }
                if (choseUri == null) return;
                startCrop(choseUri);
                break;
        }
    }

    private void startCrop(Uri uri) {
        LogUtils.d(mContext, "开始裁剪");
        imagePath = PathUtils.randomCacheImage(mContext);
        OsUtils.startPictureCrop(mContext, uri, imagePath, 480, 600, 0, 0);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (isFinishing() || resultCode != Activity.RESULT_OK) {
            return;
        }
        if (requestCode == OsUtils.REQUEST_CODE_CAMERA) {
            File file = new File(imagePath);
            if (!file.exists()) return;
            choseUri = PathUtils.fileToUri(file);
            dealWithPicture();
        } else if (requestCode == OsUtils.REQUEST_CODE_PICK_IMAGE) {
            if (data != null) {
                choseUri = data.getData();
                dealWithPicture();
            }
        } else if (requestCode == OsUtils.REQUEST_CODE_CROP) {
            File file = new File(imagePath);
            if (!file.exists()) return;
            choseUri = PathUtils.fileToUri(file);
            dealWithPicture();
        }
    }

    /**
     * 处理返回的照片，可以在这里对不同的ImageView做处理
     */
    private void dealWithPicture() {
        Bitmap bitmap = ImageUtils.getBitmapFromUri(mContext, choseUri);
        if (bitmap == null) return;
        Bitmap newBitmap = ImageUtils.addWatermark(bitmap, bitmap.getWidth() / 20f, bitmap.getHeight() * 29f / 30,
                "Jorelanbo", 120, Color.parseColor("#FF0000"));
        ImageUtils.qualityCompress(newBitmap, new File(imagePath), 100);
//        Glide.with(mContext).load(new File(imagePath)).into(iv);
        ImageLoader.get(mContext).display(choseUri.toString()).into(iv);
    }
}
